$(function () {
    $("#jqGrid").jqGrid({

        datatype: 'local',
        colModel: [
            {label: '交易id', name: 'transactionId',width: 45,key: true,align: 'center',sortable: false
            },
            {label: '交易金额', name: 'amount', width: 75, align: 'center', sortable: false},
            {label: 'SN', name: 'sn', width: 100, align: 'center', sortable: false},
            {label: '终端号', name: 'merchantCode', width: 100, align: 'center', sortable: false},
            {label: '终端类型', name: 'terminalName', width: 45, align: 'center', sortable: false},
            {
            label: '交易时间', name: 'transactionDate', width: 75, align: 'center', sortable: false
            },
            {label: '导入时间', name: 'createDate', width: 80, align: 'center', sortable: false},
            {label: '是否vip', name: 'isVip', width: 75, align: 'center', sortable: false},

            {
                label: '是否线上', name: 'isOnline', width: 75, align: 'center', formatter: function (value, options, row) {
                    return value == 0 ? "否" : "是";
                }, sortable: false
            },
            {
                label: '是否分润', name: 'isBenefited', width: 75, align: 'center', formatter: function (value, options, row) {
                    return value == 0 ? "否" : "是";
                }, sortable: false
            },
            {label: '终端归属', name: 'name', width: 75, align: 'center', sortable: false},
            {label: '归属人手机', name: 'phone', width: 75, align: 'center', sortable: false},

        ],
        viewrecords: true,
        footerrow: true,
        height: 400,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 60,
        autowidth: true,
        multiselect:true,
        multiboxonly:true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    }).setGridParam();
    bindheaderType();
    initDown();
});
//计算交易总额，置于表格底部
function sumAmount() {
    let sumAmount = $("#jqGrid").getCol('amount', false, 'sum');
    sumAmount = sumAmount.toFixed(2);
    if (vm.isClear) {
        $("#jqGrid").footerData('set', {
            transactionId: '当前页总计:', amount: "<span style='color:red;'>0.00</span>",
            sn: "所有金额总计", merchantCode: "<span style='color:red;'>0.00</span>"
        });
    }else{
        $.ajax({
            url: baseURL + 'sys/transactionInfo/queryTotal',
            data: JSON.stringify(vm.q),
            dataType: "json",
            contentType: "application/json",
            type: "post",
            success: function (r) {
                $("#jqGrid").footerData('set', {
                    transactionId: '当前页总计:', amount: "<span style='color:red;'>" + sumAmount + "</span>",
                    sn: "所有金额总计", merchantCode: "<span style='color:red;'>" + r.totalAmount + "</span>"
                });
            }
        })
    }


}

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            startDate: null,
            sn: null,
            endDate: null,
            name: null,
            phone: null,
            merchantCode:null,
            terminalType:0,
            isBenefited:-1
        },
        showList: true,
        isClear: false,
        title: null,
        terminalTypes:[],
        terminalType: 0,
        temps:[],
        showXTB:true

    },
    methods: {
        query: function () {
            vm.reload();
        },
        clean: function () {
            vm.showList = true;
            vm.isClear = true;
            vm.q = {
                startDate: null,
                sn: null,
                endDate: null,
                name: null,
                phone: null,
                merchantCode:null,
                terminalType:0,
                isBenefited:-1
            };
            $("#startDate").val("");
            $("#jqGrid").jqGrid('clearGridData');
            $("#endDate").val('');
        },

        reload: function () {
            vm.showList = true;
            vm.isClear = false;
            vm.q.startDate = $("#startDate").val();
            vm.q.endDate = $("#endDate").val();
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'startDate': vm.q.startDate,
                    'endDate': vm.q.endDate,
                    'sn': vm.q.sn,
                    "name": vm.q.name,
                    'phone': vm.q.phone,
                    'terminalType':vm.q.terminalType,
                    'merchantCode':vm.q.merchantCode,
                    'isBenefited':vm.q.isBenefited
                },
                datatype: 'json',
                url: baseURL + 'sys/transactionInfo/list',
                page: 1,
                gridComplete: sumAmount
            }).trigger("reloadGrid");
        },
        del:function (message) {

            let rowIds=getSelectedRows();
            console.log(rowIds)
            if(rowIds==null){
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/transactionInfo/deleteTran",
                    contentType: "application/json",
                    data: JSON.stringify(rowIds),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function () {
                                vm.reload();
                            });
                        } else {
                            let list = r.list;
                            let str = "";
                            for (let i = 0; i < list.length; i++) {

                                if (i == list.length - 1) {
                                    str += list[i]
                                } else {
                                    str += list[i] + ","
                                }
                            }
                            alert("id为" + str + "的交易已计算分润无法删除");
                        }
                        vm.reload();
                    }
                });
            });


        }
    }
});




function bindheaderType() {

        $.getJSON(baseURL + "sys/terminalType/list", function (r) {
            vm.$set(vm.terminalTypes, 0, {
                terminalType: 0,
                terminalName: "请选择类型"
            });
            for (var i = 0; i < r.page.length; i++) {
                vm.$set(vm.terminalTypes, (i + 1), r.page[i]);
            }
            vm.q.terminalType = 0;
        })
}

function opendownload() {

    layer.open({
        type: 1,
        title: "交易模板",
        content: $("#downLoad"),
        area: ['500px', 'auto'],
        btn: ['确定'],
        btn1: function (index) {
            layer.close(index);
        }

    })
}
function initDown() {
    $.get(baseURL+"sys/template/getTemplatesByDept",r=>{
        for (var i = 0; i < r.list.length; i++) {
            vm.$set(vm.temps, i , r.list[i]);
        }
    })
}